<template>
	<view>
		<u-input
		  class="underline-input"
		  placeholder="请输入旧邮箱"
		  type="text"
		  v-model="oldEmail"
		  clearable
		  autoBlur
		 readonly="true"
		></u-input>
		<u-input
		  class="underline-input"
		  placeholder="请输入验证码"
		  type="text"
		  v-model="verificationCode"
		  clearable
		  autoBlur
		></u-input>
		<u-button @click="getVerificationCode()" text="发送验证码"></u-button>
		<u-button @click="next()" text="下一步"></u-button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				oldEmail:'',
				verificationCode:'',
				
				uid:'',
				token:'',
				
			}
		},
		methods: {
			initPage(){
				this.uid=uni.getStorageSync('UID');
				this.token=uni.getStorageSync('token');
				console.log(this.uid);
			if(this.uid!=''&&this.token!=''){
				this.getEmail();
				//this.getVerificationCode();
			}
				
			},
		    getVerificationCode() {
		       console.log('注册邮箱:', this.oldEmail, '获取验证码');
		       uni.showLoading({
		         title: 'Loading',
		         mask: true
		       });
		       uni.request({
		         url: this.baseUrl + '/getEmailVerifyCode',
		         method: 'POST',
		         data: {
		           data: {
		             email: this.oldEmail,
		           },
		           msgType: 'getEmailVerifyCode'
		         },
		         header: {
		           'Content-Type': 'application/json',
				   uid: this.uid, // 用户唯一标识
				   token: this.token // 身份认证 Token
		         },
		         success: (res) => {
		           uni.hideLoading();
		           if (res.data.code == 1) {
		             uni.showToast({
		               title: res.data.data,
		               icon: 'success',
		               duration: 2000
		             });
					 console.log(res.data.data);

		           } else {
		             uni.showToast({
		               title: res.data.msg,
		               icon: 'none',
		               duration: 2000
		             });
		           }
		         },
		         fail: (err) => {
		           uni.hideLoading();
		           uni.showToast({
		             title: err,
		             icon: 'none',
		             duration: 2000
		           });
		         }
		       });
		     },
		    // 验证邮箱格式的方法
		    validateEmail(email) {
		      const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
		      return emailRegex.test(email);
		    },
			next(){
				if (!this.validateEmail(this.oldEmail)) {
				console.log('邮箱格式不正确');
				uni.showToast({
				title:'邮箱格式不正确',
				icon:'error',
				duration:2000
				});
				return;
				}
      
      
		uni.request({
			url:this.baseUrl+'/changeAccount',
			method:'POST',
			data:{
				data:{
					email:this.oldEmail,
					code:this.verificationCode
				},
				msgType:'changeAccount'
			},
			header: {
				'Content-Type': 'application/json' ,// 如果需要以JSON格式发送数据
				uid: this.uid, // 用户唯一标识
				token: this.token // 身份认证 Token
			},
			success: (res) => {
				uni.hideLoading();
				//console.log(res);
				if(res.data.code==1){
					uni.showToast({
						title:res.data.data,
						icon:'none',
						duration:2000
					});
					uni.navigateTo({
						url:'/pages/setting/accountSetting/updateAccount/updateAccount'
					})
				}else if(res.data.code==0){
					uni.showToast({
						title:res.data.msg,
						icon:'none',
						duration:2000
					});
				}
				else if(res.data.code==3){
					uni.showToast({
						title:'none',
						icon:'none',
						duration:2000
					});
					
					
				}
				else{
					uni.showToast({
						title:'系统错误，请联系我们',
						icon:'none',
						duration:2000
					});
				}
				
			},
			fail: (err) => {
				uni.hideLoading();
				console.log(err);
			}
		})
      
    },
			startCountdown() {
			  this.isCountingDown = true;
			  this.countdown = this.countdownConfig; // 重置倒计时为配置值
			    
			  const interval = setInterval(() => {
			    this.countdown--;
			    if (this.countdown <= 0) {
			      clearInterval(interval);
			      this.isCountingDown = false; // 倒计时结束
			    }
			  }, 1000);
			},
			    
			flipCard() {
			  this.isFlipped = !this.isFlipped;
			  if (this.isFlipped) {
			    const nowTime = new Date();
			    const lastGetEmailVerifyCodeTime = uni.getStorageSync("lastGetEmailVerifyCodeTime");
			    
			    // 确保上次获取验证码的时间存在
			    if (lastGetEmailVerifyCodeTime) {
			      const timeDifference = (nowTime - new Date(lastGetEmailVerifyCodeTime)) / 1000; // 转换为秒
			      if (timeDifference < this.countdownConfig) {
			        this.countdown = this.countdownConfig - Math.floor(timeDifference);
			        this.startCountdown(); // 启动倒计时
			      }
			    }
			  }
			},
			
			getEmail() {
	    // 发起网络请求，获取当前用户的旧邮箱
	    uni.request({
	       	url:this.baseUrl+'/getEmail',
	        method: 'POST', // 请求方法
	        data: {
	            data: {
	                userId: this.uid // 用户唯一标识
	            },
	            msgType: 'getEmail' // 消息类型，用于后端区分请求
	        },
	        header: {
	            'Content-Type': 'application/json', // 指定请求头类型
	            uid: this.uid, // 用户唯一标识
	            token: this.token // 身份认证 Token
	        },
	        success: (res) => {
				console.log("接口返回数据:", res);
				console.log("接口返回数据:", res.data.data.email);
	        	this.oldEmail=res.data.data.email;
	        	uni.setStorageSync('email',res.data.data.email);
	        },
	        fail: (err) => {
	        	console.log(err);
	        },
	    });
	}

		},
		mounted() {
			this.initPage()
		}
	}
</script>

<style>

</style>
